package com.wcity.manage.web.module;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.UserModuleMapper;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.dao.model.UserModule;
import com.wcity.manage.service.ModuleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by jn on 2016/12/9.
 * 用户自定义模块ctrl
 */
@Controller
public class UserModuleAct {

    /**
     * 用户自定义模块修改
     * @param module
     * @param response
     */
    @RequestMapping("user_module_edit")
    public void userModuleEdit(UserModule module, HttpServletResponse response){

        String errorMsg = moduleService.editUserModule( module );

        if ( StringUtil.isBlank( errorMsg ) ) {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "修改成功", "user_module_list", "", "closeCurrent", "" ) );
        } else {
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "300", errorMsg, "", "", "", "" ) );
        }

    }

    /**
     * 用户自定义模块修改跳转
     * @param id
     * @return
     */
    @RequestMapping("to_user_module_edit")
    public ModelAndView toUserModuleEdit(String id){
        UserModule module = userModuleMapper.find( Integer.parseInt( id ) );

        ModelAndView mav = new ModelAndView("module/usermoduleedit");
        mav.addObject("module",module);
        return mav;

    }

    /**
     * 用户自定义模块列表
     * @param request
     * @param pageModel
     * @param module
     * @return
     */
    @RequestMapping("user_module_list")
    public ModelAndView userModuleList(HttpServletRequest request, PageModel<UserModule> pageModel, UserModule module){

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        module.setUserId( user.getId() );
        pageModel.setParams( module );

        List<UserModule> list = userModuleMapper.findListPage( pageModel );
        ModelAndView mav = new ModelAndView("module/usermodulelist");
        mav.addObject("list",list);
        mav.addObject("pageModel",pageModel);
        mav.addObject("module",module);
        return mav;
    }

    @Resource private ModuleService moduleService;
    @Resource private UserModuleMapper userModuleMapper;

}
